package com.wei.czz.framework.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.blog.dao.BlogTagDao;
import com.wei.czz.framework.blog.entity.BlogTagEntity;
import com.wei.czz.framework.blog.service.BlogTagService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2025-01-21 21:21:43
 * className: BlogTagServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogTagService")
public class BlogTagServiceImpl extends ServiceImpl<BlogTagDao, BlogTagEntity> implements BlogTagService {

    private static final Logger log = LoggerFactory.getLogger(BlogTagServiceImpl.class);

    @Transactional
    @Override
    public void batchSave(List<BlogTagEntity> blogTagList) {
        boolean bool = this.saveBatch(blogTagList);
        log.info("批量保存博客标签完成。bool={}", bool);
    }

    @Override
    public List<BlogTagEntity> findList(Long blogId) {
        return this.findList(blogId, null);
    }

    @Override
    public List<BlogTagEntity> findList(Long blogId, Integer version) {
        LambdaQueryWrapper<BlogTagEntity> blogTagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogTagLambdaQueryWrapper.eq(BlogTagEntity::getBlogId, blogId)
                .eq(Objects.nonNull(version), BlogTagEntity::getVersion, version)
                .eq(BlogTagEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogTagEntity> blogTagList = baseMapper.selectList(blogTagLambdaQueryWrapper);
        log.info("查询博客标签完成。list.size={}", blogTagList.size());
        return blogTagList;
    }

    @Override
    public List<BlogTagEntity> findList(List<Long> blogIdList) {
        if (CollectionUtils.isEmpty(blogIdList)) {
            log.info("获取博客关联博客标签列表，传递的博客主键列表为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<BlogTagEntity> blogTagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogTagLambdaQueryWrapper.in(BlogTagEntity::getBlogId, blogIdList)
               .eq(BlogTagEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<BlogTagEntity> blogTagList = baseMapper.selectList(blogTagLambdaQueryWrapper);
        log.info("【获取博客关联博客标签列表】查询完成。list.size={}", blogTagList.size());

        return blogTagList;
    }

    @Override
    public List<String> findNameList(Long blogId) {
        // 查询博客标签
        List<BlogTagEntity> blogTagList = this.findList(blogId);
        // 列表映射
        return blogTagList.stream()
                .sorted(Comparator.comparing(BlogTagEntity::getSort))
                .map(BlogTagEntity::getTagName)
                .collect(Collectors.toList());
    }

    @Transactional
    @Override
    public void batchUpdate(List<BlogTagEntity> updateBlogTagList) {
        boolean bool = this.updateBatchById(updateBlogTagList);
        log.info("批量更新博客标签完成。bool={}", bool);
    }

    @Override
    public void delete(Long blogId, Integer version) {
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<BlogTagEntity> blogTagLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置字段值
        blogTagLambdaUpdateWrapper.set(BlogTagEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(BlogTagEntity::getUpdateTime, date)
                .set(BlogTagEntity::getUpdateUser, userPo.getUsername())
                .set(BlogTagEntity::getUpdateUserId, userPo.getUserId());
        // 匹配条件
        blogTagLambdaUpdateWrapper.eq(BlogTagEntity::getBlogId, blogId)
               .eq(BlogTagEntity::getVersion, version)
              .eq(BlogTagEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(blogTagLambdaUpdateWrapper);
        log.info("删除博客标签完成。count={}", count);
    }
}